﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CML.Services.Interfaces;
using CML.Data;
using System.IO;

namespace CML.EXE
{
	class Program
	{
		static void Main(string[] args)
		{
			var c = Configuration.Depencency.RegisterDependencies();
			var intrepreterService = c.GetInstance<ICMLInterpreterService>();
			var fileService = c.GetInstance<IFileService>();

			if (args.Where(h => h.ToLower() == "help").FirstOrDefault() != null)
			{
				RenderHelp();
				return;
			}

			var a = new Arguments(args);


			if (string.IsNullOrEmpty(a.script))
				throw new Exception("No script (file path) parameter provided");

			if (string.IsNullOrEmpty(a.source))
				throw new Exception("No source (folder path) parameter provided");


			var cml = fileService.GetFileContent(a.script);

			intrepreterService.RunScript(new RunScriptRequest() { 
				sourceFolder= a.source,
				targetFolder=a.target,
				cml = cml,
				writeTarget = !string.IsNullOrEmpty(a.target)
			});
		}

		static void RenderHelp()
		{
			Console.WriteLine("-----------CML Help-----------");
			Console.WriteLine("");
			Console.WriteLine("CML is invoked in the following format:");
			Console.WriteLine("	cml arg1 arg2 ... argN");
			Console.WriteLine("");
			Console.WriteLine("Each argument is a key=value pair representing a parameter");
			Console.WriteLine("");
			Console.WriteLine("The following parameter are available:");
			Console.WriteLine("	script=<path to script file>");
			Console.WriteLine("	source=<path to source file>");
			Console.WriteLine("	target=<path to target>");
			Console.WriteLine("");
			Console.WriteLine("Samples:");
			Console.WriteLine("	cml script=script.cml source=c:\\sourcepath target=c:\\targetpath");
			Console.WriteLine("	cml script.cml source=c:\\sourcepath target=c:\\targetpath");
			Console.WriteLine("");
			Console.WriteLine("Notes:");
			Console.WriteLine("	* the 'script' and 'source' parameters are required");
			Console.WriteLine("	* the 'target' folder will be cleared of all content if it exists");
			Console.WriteLine("	* if the 'target' parameter is not supplied no output will be written");
			Console.WriteLine("	* pass 'help' as an argument to get help information: cml help");
			Console.WriteLine("	* if no key is passed the argument is assumed to be the script value");
			Console.WriteLine("");
			Console.WriteLine("-----------End Help-----------");
			Console.Read();
		}
	}
}
